package edu.olin.scheduler.common;

import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;

public class Category implements TreeNode {
    private String myName;
    private ChildEnumeration myChildren;
    private TreeNode myMommy;
    public boolean isCourse = false;
    
    public Category(String name, TreeNode parent) {
        myName=name;
        myMommy=parent;
        myChildren=new ChildEnumeration();
    }
    public Category(String name){
        this(name,null);
    }
    
    public Enumeration children() {
        return myChildren;
    }
    public void addChild(TreeNode newChild) {
        myChildren.addChild(newChild);
    }
    public boolean getAllowsChildren() {
        return true;
    }
    public TreeNode getChildAt(int childIndex) {
        return myChildren.childAt(childIndex);
    }
    public int getChildCount() {
        return myChildren.childCount();
    }
    public int getIndex(TreeNode node) {
        return myChildren.indexOf(node);
    }
    public void setParent(TreeNode newParent){ //this is new as of 29apr2007
        myMommy = newParent;
    }
    public TreeNode getParent() {
        return myMommy;
    }
    public boolean isLeaf() {
        if (getChildCount()==0)
            return true;
        else
            return false;
    }
    public String getName(){ return myName; }
    private class ChildEnumeration implements Enumeration {
        private ArrayList children;
        int counter=0;
        public ChildEnumeration() {
            children=new ArrayList();
        }
        public void addChild(TreeNode child) {
            children.add(child);
        }
        public TreeNode childAt(int index) {
            return (TreeNode)children.get(index);
        }
        public int childCount() {
            return children.size();
        }
        public int indexOf(TreeNode child) {
            return children.indexOf(child);
        }
        public boolean hasMoreElements() {
            if (childCount()>counter)
                return true;
            else
                return false;
        }
        public TreeNode nextElement() {
            counter++;
            return (TreeNode)children.get(counter-1);
        }
    }
}